package com.ourclass.app;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.ourclass.R;
import com.ourclass.R.id;
import com.ourclass.R.layout;
import com.ourclass.R.menu;
import com.ourclass.adapter.AdapterDetalhesAula;
import com.ourclass.model.Aluno;
import com.ourclass.model.Disciplina;

import android.app.Activity;
import android.app.ActionBar;
import android.app.DatePickerDialog;
import android.app.Fragment;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.os.Build;

public class RegistrarAulaActivity extends Activity implements OnItemClickListener{
	
	private DatePickerDialog dialog = null;
	private ListView list;
	private Disciplina disciplina;
	private TextView rotuloData;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_registrar_aula);

		if (savedInstanceState == null) {
			getFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
			
		chamarLista();
		carregarLista();
		
		TextView rotuloDisciplina = (TextView) findViewById(R.id.aulaDisciplina);
		rotuloDisciplina.setText(DiariosActivity.diarioSelecionado.getDisciplina().getNome());
		
		TextView rotuloCurso = (TextView) findViewById(R.id.aulaCurso);
		rotuloCurso.setText(DiariosActivity.diarioSelecionado.getDisciplina().getCurso());				
		
		rotuloData = (TextView) findViewById(R.id.aulaData);
		rotuloData.setText(new Date().toString());
		
		
		ImageButton alterarData = (ImageButton) findViewById(R.id.alterarData);
		alterarData.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				 Calendar c = Calendar.getInstance(); 								 
				 if(dialog == null){
	                  dialog = new DatePickerDialog(
	                		  v.getContext(),
	                		  new PickDate(),
	                		  c.get(Calendar.YEAR),
	                		  c.get(Calendar.MONTH),
	                		  c.get(Calendar.DAY_OF_MONTH)
	                 );	                 
				 }	 
				 dialog.show();
			}
		});
		
		Button enviarFrequencia = (Button) findViewById(R.id.enviarFrequencia);
		enviarFrequencia.setOnClickListener(new OnClickListener() {
			
			public void onClick(View arg0) {
				// TODO Auto-generated method stub				
				voltarParaAulas();
			}
		});
	}
	
	public void voltarParaAulas(){
		Intent it = new Intent(this,AulasActivity.class);
		startActivity(it);
		Toast.makeText(this,"Aula registrada!",Toast.LENGTH_SHORT).show();
	}
	
	private void chamarLista() {
		// TODO Auto-generated method stub
		list = (ListView) findViewById(R.id.listFrequencia);
		list.setItemsCanFocus(false);
		list.setFocusable(true);
		list.setFocusableInTouchMode(true);
		list.setClickable(true);
		list.setOnItemClickListener((OnItemClickListener) this);
	}
	
	private void carregarLista() {
		// TODO Auto-generated method stub
	
		
		disciplina = DiariosActivity.diarioSelecionado.getDisciplina();		
		
		//System.out.println("Quantidade de alunos:" + disciplina.getAlunosMatriculados().size());
	
		try {
			//System.out.println("Valor de alunos em teste: " + teste.getListaAlunos());
			//System.out.println("Definitivo: " + disciplina.getAlunosMatriculados().size());
			atualizarLista(disciplina.getAlunosMatriculados());
		} catch (NullPointerException e) {
			// TODO: handle exception
			e.printStackTrace();
			System.out.println("Deu valor NULO em Alunos...");
		}
	}
	
	private void atualizarLista(List<Aluno> listaAlunos) {
		// TODO Auto-generated method stub
		AdapterDetalhesAula adapter = new AdapterDetalhesAula(this, R.layout.adapter_frequencia, listaAlunos);
		list.setAdapter(adapter);
	}

	public class PickDate implements OnDateSetListener {

		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			// TODO Auto-generated method stub
			rotuloData.setText(dayOfMonth + "/" + monthOfYear + "/" + year);
			dialog.hide();
		}

	}
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.registrar_aula, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_registrar_aula,
					container, false);
			return rootView;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		
	}

}
